﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.ApplicationBlocks.Data;
using System.Data;

using YouHP.Model;
using YouHP.IDAL;
using System.Data.SqlClient;


namespace YouHP.SqlServerDAL
{
    public class AdminDAL : IAdmin
    {

        #region IAdmin 成员

        /// <summary>
        /// 增加新管理员账户
        /// </summary>
        /// <param name="info">管理员信息对象</param>
        /// <returns>如果操作成功则返回True,否则返回False</returns>
        public bool CreateNewAdmin(AdminInfo info)
        {
            try
            {
                return SqlHelper.ExecuteNonQuery(Common.ConnStr, Common.AdminSP_AddAdmin, info.Name, info.Password) > 0;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 删除管理员账户
        /// </summary>
        /// <param name="info">管理员信息对象</param>
        /// <returns>如果操作成功则返回True,否则返回False</returns>
        public bool DeleteAdminAccount(AdminInfo info)
        {
            try
            { 
                SqlHelper.ExecuteNonQuery(Common.ConnStr, Common.AdminSP_DelAdmin,info.ID);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 读取管理员信息
        /// </summary>
        /// <param name="info">管理员信息对象</param>
        /// <returns>管理员信息对象</returns>
        public AdminInfo GetAdminByNameAndPassword(AdminInfo info)
        {
            try
            {
                SqlParameter[] sqlParameters = new SqlParameter[] 
                {
                    new SqlParameter("@Name",SqlDbType.NVarChar, 50),
                    new SqlParameter("@Password",SqlDbType.NVarChar, 50)
                };
                sqlParameters[0].Value = info.Name;
                sqlParameters[1].Value = info.Password;
                IDataReader gottenInfo = SqlHelper.ExecuteReader(
                    Common.ConnStr,
                    CommandType.StoredProcedure,
                    Common.AdminSP_GetAdmin,
                    sqlParameters) as IDataReader;
                return CBO.FillObject<AdminInfo>(gottenInfo);
            }
            catch 
            {
                return info;
            }
        }

        /// <summary>
        /// 更新管理员账户
        /// </summary>
        /// <param name="info">管理员信息对象</param>
        /// <returns>如果操作成功则返回True,否则返回False</returns>
        public bool UpdateAdminPassword(AdminInfo info)
        {
            try
            {
                return SqlHelper.ExecuteNonQuery(Common.ConnStr, Common.AdminSP_UpdateAdmin, info.ID, info.Password) > 0;
            }
            catch
            {
                return false;
            }
        }

        #endregion
    }
}
